package com.kxmall.web.controller.rider.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.rider.domain.KxRiderItem;
import com.kxmall.rider.domain.bo.KxRiderItemBo;
import com.kxmall.rider.domain.vo.KxRiderItemVo;
import com.kxmall.rider.mapper.KxRiderItemMapper;
import com.kxmall.web.controller.rider.service.IKxRiderItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 配送商品信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-09-21
 */
@RequiredArgsConstructor
@Service
public class KxRiderItemServiceImpl implements IKxRiderItemService {

    private final KxRiderItemMapper baseMapper;

    /**
     * 查询配送商品信息
     */
    @Override
    public KxRiderItemVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询配送商品信息列表
     */
    @Override
    public TableDataInfo<KxRiderItemVo> queryPageList(KxRiderItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxRiderItem> lqw = buildQueryWrapper(bo);
        Page<KxRiderItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询配送商品信息列表
     */
    @Override
    public List<KxRiderItemVo> queryList(KxRiderItemBo bo) {
        LambdaQueryWrapper<KxRiderItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxRiderItem> buildQueryWrapper(KxRiderItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxRiderItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRiderOrderId() != null, KxRiderItem::getRiderOrderId, bo.getRiderOrderId());
        lqw.like(StringUtils.isNotBlank(bo.getSpuName()), KxRiderItem::getSpuName, bo.getSpuName());
        lqw.eq(StringUtils.isNotBlank(bo.getAmount()), KxRiderItem::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), KxRiderItem::getUrl, bo.getUrl());
        lqw.eq(bo.getUnitPrice() != null, KxRiderItem::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getPreferentialPrice() != null, KxRiderItem::getPreferentialPrice, bo.getPreferentialPrice());
        return lqw;
    }

    /**
     * 新增配送商品信息
     */
    @Override
    public Boolean insertByBo(KxRiderItemBo bo) {
        KxRiderItem add = BeanUtil.toBean(bo, KxRiderItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改配送商品信息
     */
    @Override
    public Boolean updateByBo(KxRiderItemBo bo) {
        KxRiderItem update = BeanUtil.toBean(bo, KxRiderItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxRiderItem entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除配送商品信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
